package com.softmarket.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginFilter implements Filter {

	/**
	 * {@link LoginFilter}を作成します。
	 */
	public LoginFilter() {
	}

	public void init(FilterConfig config) throws ServletException {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest req = (HttpServletRequest) request;
		String uri = req.getRequestURI();
		if (uri.indexOf("login") < 0 && uri.indexOf(".css") < 0
				&& uri.indexOf(".js") < 0) {
			Object loginFlg = req.getSession().getAttribute("loginFlg");
			if (!"true".equals(loginFlg)) {
				((HttpServletResponse) response).sendRedirect(req
						.getContextPath()
						+ "/manager/login/");
				return;
			}
		}

		chain.doFilter(request, response);
		return;
	}
}